//
//  HomeListMultipleCell.swift
//  MT
//
//  Created by 陈悦 on 2020/5/13.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory
import SnapKit
import Kingfisher
class HomeListMultipleCell: UITableViewCell {
    private lazy var authProvider: MTProvider<DDCAuthAPI> = {
        let provider = MTProvider<DDCAuthAPI>()
        return provider
    }()
    
    /// 左边展示模型
    var model: HomeListDetailModel? {
        didSet {
            guard let model = model else { return }
            /// 封面图
            if let urlString = model.imgs?.first?.href {
                coverImageViewLeft.kf.setImage(with: URL(string: urlString), placeholder: UIImage(named: "placeholder-rectangle"))
            }
            /// 用户头像
            guard let headerURL = model.headerUrl else { return }
            headerImageViewLeft.kf.setImage(with: URL(string:headerURL))
//            userNameButton.setTitle(model.nickname, for: .normal)
//            /// 用户信息 Label
//            userInfoLabel.text = "\(model.age ?? 0)岁 | \(model.area ?? "")"
//
//            pauseIconButton.isHidden = !model.isContainVideo
//
//            if model.giftPrice > 0 {
//                giftContainerView.isHidden = false
//                let text = "收到\(model.giftPrice)点券"
//                flowerGiftLabel.text = text
//            } else {
//                giftContainerView.isHidden = true
//            }
            
//            addFriendButton.isHidden = model.isFriend
        }
    }
    
        /// 右边展示模型
        var rightmodel: HomeListDetailModel? {
            didSet {
                guard let model = rightmodel else { return }
                /// 封面图
                if let urlString = model.imgs?.first?.href {
                    coverImageViewRight.kf.setImage(with: URL(string: urlString), placeholder: UIImage(named: "placeholder-rectangle"))
                }
                /// 用户头像
                guard let headerURL = model.headerUrl else { return }
                headerImageViewRight.kf.setImage(with: URL(string:headerURL))
    //            userNameButton.setTitle(model.nickname, for: .normal)
    //            /// 用户信息 Label
    //            userInfoLabel.text = "\(model.age ?? 0)岁 | \(model.area ?? "")"
    //
    //            pauseIconButton.isHidden = !model.isContainVideo
    //
    //            if model.giftPrice > 0 {
    //                giftContainerView.isHidden = false
    //                let text = "收到\(model.giftPrice)点券"
    //                flowerGiftLabel.text = text
    //            } else {
    //                giftContainerView.isHidden = true
    //            }
                
    //            addFriendButton.isHidden = model.isFriend
            }
        }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeUI()
        makeConstraints()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// 封面图
    lazy var coverImageViewLeft: UIImageView = {
        let imgView = UIImageView()
        imgView.tag = 8800
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        imgView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(homeListMultipleCell(didTapCoverImageView:)))
        imgView.addGestureRecognizer(tap)
        return imgView
    }()

    /// 视频暂停 / 开始按钮
    lazy var pauseIconButtonLeft: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "icon_start"), for: .normal)
        btn.setImage(UIImage(named: "icon_stop-1"), for: .selected)
        btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action: #selector(homeListMultipleCell(didClickPlayPauseButton:)), for: .touchUpInside)
        return btn
    }()
    
    /// 是否在线
    lazy var onlineStatusLabelLeft: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex: 0xffc000)
        label.backgroundColor = UIColor(hex:0x000000).withAlphaComponent(0.5)
        label.text = "在线可聊"
        label.textAlignment = .center
        return label
    }()
    
    /// 头像
    lazy var headerImageViewLeft: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        imgView.layer.cornerRadius = 10
        imgView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(homeListMultipleCell(didTapCoverImageView:)))
        imgView.addGestureRecognizer(tap)
        return imgView
    }()
    
    /// 状态
    lazy var statusLabelLeft: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex: 0xffffff)
        label.text = "当前在线"
        label.textAlignment = .right
        return label
    }()
    
    /// 封面图
    lazy var coverImageViewRight: UIImageView = {
        let imgView = UIImageView()
        imgView.tag = 8800
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        imgView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(homeListMultipleCell(didTapCoverImageView:)))
        imgView.addGestureRecognizer(tap)
        return imgView
    }()

    /// 视频暂停 / 开始按钮
    lazy var pauseIconButtonRight: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "icon_start"), for: .normal)
        btn.setImage(UIImage(named: "icon_stop-1"), for: .selected)
        btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action: #selector(homeListMultipleCell(didClickPlayPauseButton:)), for: .touchUpInside)
        return btn
    }()
    
    /// 是否在线
    lazy var onlineStatusLabelRight: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex: 0xffc000)
        label.backgroundColor = UIColor(hex:0x000000).withAlphaComponent(0.5)
        label.text = "在线可聊"
        label.textAlignment = .center
        return label
    }()
    
    /// 头像
    lazy var headerImageViewRight: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        imgView.layer.cornerRadius = 10
        imgView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(homeListMultipleCell(didTapCoverImageView:)))
        imgView.addGestureRecognizer(tap)
        return imgView
    }()
    
    /// 状态
    lazy var statusLabelRight: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex: 0xffffff)
        label.text = "当前在线"
        label.textAlignment = .right
        return label
    }()
}

// MARK: ------------------ Initialize ----------------------
extension HomeListMultipleCell {
    
    /// UI 初始化
    private func initializeUI() {
        contentView.backgroundColor = UIColor(hex:0x000000)
        selectionStyle = .none
        contentView.addSubview(coverImageViewLeft)
        contentView.addSubview(pauseIconButtonLeft)
        coverImageViewLeft.addSubview(headerImageViewLeft)
        coverImageViewLeft.addSubview(onlineStatusLabelLeft)
        coverImageViewLeft.addSubview(statusLabelLeft)
        
        contentView.addSubview(coverImageViewRight)
        contentView.addSubview(pauseIconButtonRight)
        coverImageViewRight.addSubview(headerImageViewRight)
        coverImageViewRight.addSubview(onlineStatusLabelRight)
        coverImageViewRight.addSubview(statusLabelRight)
    }
    
    /// 约束
    private func makeConstraints() {
        coverImageViewLeft.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
            make.width.equalTo(ScreenWidth/2-2)
            make.bottom.equalTo(contentView).offset(-3)
        }
        
        onlineStatusLabelLeft.snp.makeConstraints { (make) in
            make.left.top.equalTo(5)
            make.width.equalTo(60)
            make.height.equalTo(16)
        }
        
        headerImageViewLeft.snp.makeConstraints { (make) in
            make.left.equalTo(5)
            make.bottom.equalTo(-5)
            make.width.height.equalTo(20)
        }
        
        statusLabelLeft.snp.makeConstraints { (make) in
            make.bottom.right.equalTo(-5)
            
        }
        
        pauseIconButtonLeft.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(coverImageViewLeft).offset(-12)
            make.width.height.equalTo(55)
        }
        
        coverImageViewRight.snp.makeConstraints { (make) in
            make.right.top.equalToSuperview()
            make.width.equalTo(ScreenWidth/2-2)
            make.bottom.equalTo(contentView).offset(-3)
        }
        
        onlineStatusLabelRight.snp.makeConstraints { (make) in
            make.left.top.equalTo(5)
            make.width.equalTo(60)
            make.height.equalTo(16)
        }
        
        headerImageViewRight.snp.makeConstraints { (make) in
            make.left.equalTo(5)
            make.bottom.equalTo(-5)
            make.width.height.equalTo(20)
        }
        
        statusLabelRight.snp.makeConstraints { (make) in
            make.bottom.right.equalTo(-5)
            
        }
        
        pauseIconButtonRight.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(coverImageViewLeft).offset(-12)
            make.width.height.equalTo(55)
        }
        
    }
    
}

// MARK: ------------------ 点击方法 ----------------------
@objc
extension HomeListMultipleCell {
    /// 点击播放按钮
    private func homeListMultipleCell(didClickPlayPauseButton sender: UIButton) {
//        if !(model?.isContainVideo ?? false) {
//            return
//        }
//        sender.isSelected = !sender.isSelected
//        if let delegate = delegate {
//            delegate.homeListCellDidClickPlayPauseButton?(self, isPlay: !sender.isSelected)
//        }
    }
    
    /// 点击封面图
    private func homeListMultipleCell(didTapCoverImageView gestureRecognizer: UITapGestureRecognizer) {
        var dict: [String:Any]?
        if let userName = model?.userName {
            dict = ["username" : userName]
        }
        NotificationCenter.default.post(name: GlobalNotificationName.homeInteractionUserInfo.name, object: nil, userInfo: dict)
//        var dict1: [String:Any]?
//        if let userName = model?.userName {
//            dict1 = ["imUserName" : userName, "isFriend" : model?.isFriend ?? false]
//        }
//        NotificationCenter.default.post(name: GlobalNotificationName.homeInteractionSendGift.name, object: nil, userInfo: dict1)
    }
}
